Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
The has-ansi npm package is used to check if a string contains ANSI escape codes. These codes are often used to format output in terminal applications, such as changing colors, styles, or other text attributes. This package provides a simple way to detect the presence of these codes in a given string.
Check for ANSI escape codes in a string
This feature allows you to check if a string contains ANSI escape codes. The function `hasAnsi` returns `true` if the string contains any ANSI escape codes, and `false` otherwise. This is useful for determining whether text formatting needs to be stripped or processed differently.
"const hasAnsi = require('has-ansi');\nconsole.log(hasAnsi('\u001B[4mUnicorn\u001B[0m')); // true\nconsole.log(hasAnsi('Unicorn')); // false"
Chalk is a popular npm package for styling terminal text. Unlike has-ansi, which is used to detect ANSI codes, Chalk is used to apply styles such as color, background color, and text formatting (bold, italic) through chainable methods. Chalk focuses on creating styled text, while has-ansi is about detection.
Strip-ansi is a package that removes ANSI escape codes from a string. It complements has-ansi by providing the functionality to clean a string of any ANSI codes that has-ansi can detect. While has-ansi checks for the presence of ANSI codes, strip-ansi is used to remove them.
Ansi-regex is a package that provides a regular expression to match ANSI escape codes. It can be used for a similar purpose as has-ansi, but with a more manual approach, giving developers the flexibility to use the regex in various contexts. Has-ansi offers a simpler and more direct API for detection.
Check if a string has ANSI escape codes
$ npm install has-ansi
import hasAnsi from 'has-ansi';
hasAnsi('\u001B[4mUnicorn\u001B[0m');
//=> true
hasAnsi('cake');
//=> false
FAQs
Check if a string has ANSI escape codes
The npm package has-ansi receives a total of 9,754,629 weekly downloads. As such, has-ansi popularity was classified as popular.
We found that has-ansi demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.